package com.itheima.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.mapper.SetMealDao;
import com.itheima.Service.SetMealService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    @Transactional
    public void add(Setmeal setmeal, List<Integer> checkgroupIds) throws Exception {
       if ( setMealDao.findByName(setmeal.getName()) != null)
       {
           throw new RuntimeException("该套餐已存在");
       }
       setMealDao.add(setmeal);


        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.addSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
        redisTemplate.opsForValue().set("uploadSuccess", setmeal.getImg());

      //  generateMobileStaticHtml();
    }

    @Override
    @Transactional
    public void edit(Setmeal setmeal, List<Integer> checkgroupIds) {
        //删除套餐与检查组关系
        setMealDao.deleteAssociation(setmeal.getId());
        //更新套餐
        setMealDao.update(setmeal);
        //更新套餐与检查组关系
        setMealDao.insertSetMealCheckGroup(setmeal.getId(), checkgroupIds);
    }


    @Override
    public Setmeal findById(Integer id) {
        return setMealDao.findById(id);
    }

    @Override
    public PageResult findPage(QueryPageBean page) {
        PageHelper.startPage(page.getCurrentPage(),page.getPageSize());
        List<Setmeal>setmeals=setMealDao.findPage(page);
        Page<Setmeal>setmeals1=(Page<Setmeal>)setmeals;
        PageResult pageResult =new PageResult(setmeals1.getTotal(),setmeals);
        return pageResult;
    }

    public void gnerateHtml(String templateName,String htmlPageName,Map map) throws Exception
    {
        Configuration configuration=freeMarkerConfigurer.getConfiguration();
        Writer out=null;
        Template template=configuration.getTemplate(templateName);
        String outputPath = "F:/bxg-health-parent/bxg-health-mobile/src/main/resources/mobile/pages/" + htmlPageName;
        out=new FileWriter(new File(outputPath));
        template.process(map,out);
    }

    public void generateMobileStaticHtml() throws Exception {
        List<Setmeal>list=setMealDao.findAll();

        gnerateMobileSetmealListHtml(list);


        gnerateMobileSetmealDetailHtml(list);
    }

    private void gnerateMobileSetmealDetailHtml(List<Setmeal> list) throws Exception {
        for (Setmeal setmeal : list) {
            Map map=new HashMap();
            map.put("setmeal",setmeal);
            gnerateHtml("mobile_setmeal_detail.ftl","setmeal_detail"+setmeal.getId()+".html",map);
        }
    }

    private void gnerateMobileSetmealListHtml(List<Setmeal> list) throws Exception {
        Map map=new HashMap();
        map.put("setmealList",list);
        gnerateHtml("mobile_setmeal.ftl","m3_setmeal.html",map);
    }
}
